package cn.itcast.service.impl;

import cn.itcast.dao.OrderListDao;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;

import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderListService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName OrderListServiceImpl
 * @Description TODO
 * @Author lc
 * @Date 2021/11/16 18:57
 * @Version 1.0
 */
@Service
@Slf4j
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    private OrderListDao orderListDao;

    @Override
    public PageResult findPage(OrderQueryPageBean queryPageBean) {
        if (null == queryPageBean.getQueryString()) {
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            Page<Order> page = orderListDao.findAll();
            return new PageResult(page.getTotal(), page.getResult());
        } else {
            queryPageBean.setCurrentPage(0);
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            Page<Order> page = orderListDao.findByCondition(queryPageBean.getMinTime(),
                    queryPageBean.getMaxTime(),
                    queryPageBean.getOrderType(),
                    queryPageBean.getPayStatus(),
                    queryPageBean.getQueryString());
            for (int i = 0; i < page.getResult().size(); i++) {
                Integer setmealId = page.getResult().get(i).getSetmealId();
                page.getResult().get(i).setSetmealName(orderListDao.findBySetmealId(setmealId));
            }
            return new PageResult(page.getTotal(), page.getResult());
        }
    }

    @Override
    public Boolean saveOrder(Order order) {
        try {
            String orderDate = DateUtils.parseDate2String(order.getOrderDate());
            OrderSetting byOrderDate = orderListDao.isNotReservations(orderDate);
            int reservations = byOrderDate.getReservations();
            int afterReservations = reservations + 1;
            orderListDao.updateReservations(orderDate, afterReservations);
        } catch (Exception e) {
            e.printStackTrace();
        }
        order.setId(UUID.randomUUID().toString());
        order.setOrderType("电话预约");
        return orderListDao.saveOrder(order);
    }

    @Override
    public Boolean updateOrder(Order order) {
        return orderListDao.updateOrder(order);
    }

    @Override
    public Order findById(String id) {
        return orderListDao.findById(id);
    }

    @Override
    public Boolean isNotReservations(String orderDate) {
        OrderSetting orderSetting = orderListDao.isNotReservations(orderDate);
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isExist(Order order) {
        try {
            String orderDate = DateUtils.parseDate2String(order.getOrderDate());
            Integer setmealId = order.getSetmealId();
            String idCard = order.getIdCard();
            Order isExist = orderListDao.isExist(orderDate, setmealId, idCard);
            if (null != isExist) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
